package com.ydy.utils;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ydy.annotation.AdminToken;
import com.ydy.annotation.StaffToken;
import com.ydy.annotation.UserToken;
import com.ydy.model.Auth;

public class ReflexUtil {

	public static List<Auth> scanController(String basePackage) {
		List<Auth> list = new LinkedList<Auth>();
		ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); // 不使用默认的TypeFilter
		provider.addIncludeFilter(new AnnotationTypeFilter(RequestMapping.class));// 添加包筛选过滤的注解条件
		Set<BeanDefinition> beanDefinitionSet = provider.findCandidateComponents(basePackage);// 扫描当前的包
		for (BeanDefinition b : beanDefinitionSet) {
			try {
				Class<?> clazz = Class.forName(b.getBeanClassName());// 加载该全类名的类
				RequestMapping mapping = clazz.getAnnotation(RequestMapping.class);
				String[] values = mapping.value();// url字符串数组
				String moduleName = mapping.name();
				String module = "";
				String pre = "";
				moduleName = StringUtils.isEmpty(moduleName) ? "未知模块" : moduleName;
				if (values != null && values.length >= 1) {
					module = values[0];
					pre += ("/" + values[0]);
				}
				Method[] methods = clazz.getMethods();
				Auth auth = null;
				Date now = new Date();
				for (Method m : methods) {
					auth = null;
					String[] methodValues = null;
					String name = " ";
					GetMapping getMapping = m.getAnnotation(GetMapping.class);
					if (getMapping != null) {
						methodValues = getMapping.value();
						name = getMapping.name();
					}
					PostMapping postMapping = m.getAnnotation(PostMapping.class);
					if (postMapping != null) {
						methodValues = postMapping.value();
						name = postMapping.name();
					}
					DeleteMapping deleteMapping = m.getAnnotation(DeleteMapping.class);
					if (deleteMapping != null) {
						methodValues = deleteMapping.value();
						name = deleteMapping.name();
					}
					if (methodValues != null && methodValues.length >= 1) {
						String url = pre + "/" + methodValues[0];
						auth = new Auth();
						auth.setName(url);
						auth.setRemark(name);
						auth.setModule(module);
						auth.setModuleName(moduleName);
						auth.setAdminFlag(0);
						auth.setUserFlag(0);
						auth.setStaffFlag(0);
						auth.setCreateTime(now);
					}
					if (auth == null)
						continue;
					AdminToken adminToken = m.getAnnotation(AdminToken.class);
					if (adminToken != null) {
						auth.setAdminFlag(1);
					}
					StaffToken staffToken = m.getAnnotation(StaffToken.class);
					if (staffToken != null) {
						auth.setStaffFlag(1);
					}
					UserToken userToken = m.getAnnotation(UserToken.class);
					if (userToken != null) {
						auth.setUserFlag(1);
					}
					list.add(auth);
				}
			} catch (ClassNotFoundException e) {// 找不到该类就报错，然后退出系统
			}
		}
		return list;
	}

}
